﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LMS.SMSH.WEB
{
    public partial class AdmMessageManage : System.Web.UI.Page
    {
        Int32 rowNo = 0;//行数据绑定

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // 绑定gvMessage数据
                bingGvMessage();
            }
        }

        /// <summary>
        /// 绑定gvMessage数据
        /// </summary>
        private void bingGvMessage()
        {
            var query = LMSDataContext.GetLMSDB.TB_Message.Select(message => message); 
            gvMessage.DataSource = query;
            gvMessage.DataKeyNames = new string[] { "mesId" };
            gvMessage.DataBind();
        }

        /// <summary>
        /// 全选、反选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void chkHeader_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow gr in gvMessage.Rows)
            {
                CheckBox chkItem = (CheckBox)gr.FindControl("chkItem");
                //相互变幻
                chkItem.Checked = !chkItem.Checked;
            }
        }


        /// <summary>
        /// 行数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvMessage_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                rowNo++;
                e.Row.Cells[0].Text = (gvMessage.PageIndex * gvMessage.PageSize + rowNo).ToString();

                //鼠标悬停变色
                e.Row.Attributes.Add("onmouseover", "oldBG=this.style.backgroundColor;this.style.backgroundColor='#FF1199';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=oldBG;");


                Button myDelete = (Button)e.Row.FindControl("btnMyDelete");
                myDelete.Attributes.Add("onclick", "javascript:return confirm('确定删除吗?');");
            }
        }

        /// <summary>
        /// 根据留言状态选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlMessageStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (ddlMessageStatus.SelectedIndex)
            {
                case 0:
                    //全部留言信息
                    bingGvMessage();
                    break;
                case 1:
                    //已处理回复
                    var query = LMSDataContext.GetLMSDB.TB_Message.Select(msg => msg).Where(m => m.mesStatus == "已处理");
                    gvMessage.DataSource = query;
                    gvMessage.DataKeyNames = new string[] { "mesId" };
                    gvMessage.DataBind();
                    break;
                case 2:
                    //待处理回复
                    var queryDemo = LMSDataContext.GetLMSDB.TB_Message.Select(msg => msg).Where(m => m.mesStatus == "待处理");
                    gvMessage.DataSource = queryDemo;
                    gvMessage.DataKeyNames = new string[] { "mesId" };
                    gvMessage.DataBind();
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 行命令
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvMessage_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //获取参数值
            Int32 mesID = Int32.Parse(e.CommandArgument.ToString());

            foreach (GridViewRow gr in gvMessage.Rows)
            {
                CheckBox chkItem = (CheckBox)gr.FindControl("chkItem");
                if (chkItem.Checked)
                {
                    //根据留言ID，调用存储过程，删除当前选中行数据
                    LMSDataContext.GetLMSDB.sp_DeleteMesByID(mesID);
                    LMSDataContext.GetLMSDB.SubmitChanges();
                }
            }

            // 再次绑定数据
            bingGvMessage();

        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvMessage_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvMessage.PageIndex = e.NewPageIndex;
            bingGvMessage();
        }




    }
}